/**
 * 
 * splice(index,len,[item]) 注释：该方法会改变原始数组
 * splice有3个参数，它也可以用来替换/删除/添加数组内某一个或者几个值
 * index:数组开始下标 len: 替换/删除的长度 item:替换的值，删除操作的话 item为空
 */

//增
// let a=["a",'b',"c"];
// let a_length=a.push(1);//从最后插入并且返回该数组长度
// console.log(a);
// let a_sth=a.unshift(2,33,4,4);//从最前插入,可以传入很多个,返回该数组长度
// console.log("something",a_sth);
// console.log("a",a);
// let a_2=add(a,"kevin",2);
// console.log("a_2",a_2);
// console.log("a",a);
/**
 * @function 在某个节点新增元素，不会改变原数组
 * 
 */
function add(arr,item,index){
    let temp=arr;
    arr=[];
    if(!index){
        arr.push(item);
    }else{
        for(let i=0;i<temp.length;i++){
            if(i==index){
                arr.push(item);
            }else{
                arr.push(temp[i]);
            }
        }
       
    }
    //arr=newArr;//不会改变原数组
    return arr;
}

/**
 * @function 在某个节点新增元素，改变原数组
 * 
 */
function add2(arr,item,index){
    let newArr=[];
    if(!index){
        arr.push(item);
    }else{
        for(let i=0;i<arr.length;i++){
            if(i==index){
                newArr.push(item);
            }else{
                newArr.push(arr[i]);
            }
        }
       
    }
    arr=newArr;//不会改变原数组
    return newArr;
}

//删
let a1=["a",'b',"c"];
let a1_item=a1.pop();//删除最后的元素并且返回该元素
let a1_item2=a1.shift();//删除最前面的元素并且返回该元素
function remove(index,arr){

}
//改
function change(arr,item,index){
 
    if(!index){
        arr.push(item);
    }else{
        arr.splice(arr,index,0,item);//替换元素
    }
    
    return arr;
}

//查



//let k=["a","b","c"];

//在某个位置插入元素

//删除某个位置的元素
{

}
//console.log("before",k);//["a","b","c"]
//delete(k[1]);//把位置1的值设置成undefined，长度不会变
//console.log("after",k);//["a",undefined,"c"]

// let h=["a","b","c"]
// console.log("before",h);//["a","b","c"]
// h.splice(1,1);//根据指定的位置，删除元素
// console.log("after",h);//["a","c"]

// Array.prototype.remove=function(index){
//     if(index<this.length){
//         this.splice(index,1);
//     }
// }

// let ab=["a","b","c"];
// ab.remove(1);
// console.log("ab",ab);

let b1=["a","b","c"];
b1.splice(1,0,"d");//["a","d","b","c"];
console.log(b1);
b1.splice(-1,0,"e");//["a","d","b","e","c"];
console.log(b1);
b1.splice(-2,0,"s2");//["a","d","b","s2","e","c"];
console.log(b1);
b1.splice(-50,0,"sa");//["sa","a","d","b","s2","e","c"];
console.log(b1);
b1.splice(-55,0,"s1");//["s1","sa","a","d","b","s2","e","c"];
console.log(b1);
b1.splice(100,0,"f");//["s1","sa","a","d","b","s2","e","c","f"];
console.log(b1);
b1.splice(100,0,["asd","asd"]);//["s1","sa","a","d","b","s2","e","c","f",array];
console.log(b1);

try {
    console.log(b1[100].toString());
} catch (error) {
    console.error(error);
}
